<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="lib/bootstrap.min.css">
    <style>
        * {
            padding: 0;
            margin: 0;
        }
        
        .box {
            width: 800px;
            height: 400px;
            /* background-color: pink; */
            margin: 100px auto;
        }
        
        .list {
            width: 800px;
            padding-left: 25px;
        }
        
        .checkbox,
        .login {
            padding-left: 10px;
        }
        
        .login {
            margin-left: 10px;
            margin-top: 20px;
        }
        
        h2 {
            width: 760px;
            font-weight: normal;
            height: 50px;
            border-bottom: 1px solid #ddd;
            color: #333;
            font-size: 30px;
            margin-left: 15px;
        }
    </style>
</head>

<body>
    <div class="form-horizontal box">
        <h2>
            用户登录
        </h2>
        <div class="form-group list">
            <label for="username">用户名</label>
            <input type="text" class="form-control" id="username" placeholder="请输入用户名">
        </div>
        <div class="form-group list">
            <label for="password">密码</label>
            <input type="password" class="form-control" id="password" placeholder="请输入密码">
        </div>
        <div class="checkbox">
            <label>
              <input type="checkbox" id="remember"> 30天内免登陆
            </label>
        </div>
        <button class="btn btn-primary login">登录</button>
    </div>
</body>

<script src="lib/jquery.min.js"></script>
<script src="lib/common.js"></script>
<script>
    $(".login").click(function() {
        let username = $("#username").val();
        let password = $("#password").val();
        if (!username || !password) {
            alert("请输入登录信息");
            return;
        } else {
            $.ajax({
                url: `${address}/login`,
                data: {
                    username,
                    password
                },
                success(data) {
                    if (data === "1") {
                        alert("登录失败");
                    }
                    if (data === "2") {
                        alert("用户名或密码错误");
                    } else {
                        // 30天内免登录
                        if ($("#remember").prop("checked")) {
                            let date = new Date;
                            date.setDate(date.getDate() + 30);
                            document.cookie = `message = ${data};expires=${date}`
                        }
                        // 存储到sessionStorage
                        sessionStorage.setItem("message", data);
                        location.href = `${address}/students.html`
                    }
                }
            })
        }
    })
</script>

</html>